<?php
/*******************************************
 *                  8hjie                  *
 *                                         *
 *             用户红包设置方法            *
 *                                         *
 *                 18-1-29                 *
 *                  Reload                 *
 *                                         *
 *******************************************/
namespace Home\Controller;
use Think\Controller;
class PacketController extends Controller {
    /**
     * [get_redpacket 获取用户红包]
     * @param  [type] $userid [用户ID]
     * @return [type]         [description]
     * 
     */
    public function get_redpacket($userid='',$platform_id=''){
        //分平台-》获取红包-》没有红包则获取返利方案
        $map['status']=0;
        if($platform_id){
            $map['id']=$platform_id;
            $plat_list = D('Platform')->get_one($map);
             //获取返利方案
                    $packet_list = array();
               
                    $platform_id = $plat_list['id'];
                    $map = array();
                    $map['platform_id'] = $platform_id;
                    $scheme_list = D('PlatformScheme')->get_all($map,'*',$order='id asc');
                    if($scheme_list){
                        //查询红包
                        foreach ($scheme_list as $ke => $va) {
                            $map = array();
                            $map['scheme_id'] = $va['id'];
                            $map['user_id'] = $userid;
                            $packet = D('RedPacket')->get_one($map);
                            //print_r($packet);
                            $packet_list['platform_info'] = $plat_list;
                            $packet_list['scheme'][$ke] = $va;
                            if($packet){ //有红包则显示设置的红包金额
                                $packet_list['scheme'][$ke]['p_user_award'] = $packet['user_award'];
                                $packet_list['scheme'][$ke]['p_push_award'] = $packet['push_award'];
                            }else{ //没红包直接拿投资方案作为默认值
                                $packet_list['scheme'][$ke]['p_user_award'] = $va['user_award'];
                                $packet_list['scheme'][$ke]['p_push_award'] = $va['push_award'];
                            }
                        }
                    }
                

        }else{
            $plat_list = D('Platform')->get_all($map);
             //获取返利方案
                $packet_list = array();
                foreach ($plat_list as $k => $v) {
                    $platform_id = $v['id'];
                    $map = array();
                    $map['platform_id'] = $platform_id;
                    $scheme_list = D('PlatformScheme')->get_all($map);
                    if($scheme_list){
                        //查询红包
                        foreach ($scheme_list as $ke => $va) {
                            $map = array();
                            $map['scheme_id'] = $va['id'];
                            $packet = D('RedPacket')->get_one($map);
                            $packet_list[$k]['platform_info'] = $v;
                            $packet_list[$k]['scheme'][$ke] = $va;
                            if($packet){ //有红包则显示设置的红包金额
                                $packet_list[$k]['scheme'][$ke]['p_user_award'] = $packet['user_award'];
                                $packet_list[$k]['scheme'][$ke]['p_push_award'] = $packet['push_award'];
                            }else{ //没红包直接拿投资方案作为默认值
                                $packet_list[$k]['scheme'][$ke]['p_user_award'] = $va['user_award'];
                                $packet_list[$k]['scheme'][$ke]['p_push_award'] = $va['push_award'];
                            }
                        }
                    }
                }
        }
       

       

        return($packet_list);
    }

    /**
     * [set_redpacket 设置红包]
     * @param string $platform_id [平台ID]
     * @param string $scheme_id   [方案ID]
     * @param string $user_id     [用户ID]
     * @param string $user_award  [用户返利额]
     * @param string $push_award  [投顾返利额]
     */
    public function set_redpacket($platform_id='18',$scheme_id='43',$user_id='146',$user_award='588',$push_award='140'){
        //查询投资方案
        if(!$user_id){
            $data = array('errorCode'=>2,'msg'=>'红包设置失败未找到用户00a3！');
        }
        $map['id'] = $scheme_id;
        $scheme = D('PlatformScheme')->get_one($map,$field='*',$order='id desc');
        $count = $scheme['user_award'] + $scheme['push_award'];
        if($user_award<1){
            $data = array('errorCode'=>0,'msg'=>'该红包分配给用户的金额不能低于1元！');
           return($data);
        }
        if($user_award>$count){
            $data = array('errorCode'=>0,'msg'=>'红包金额有误！');
            return($data);
        }
        if($push_award<0){
            $data = array('errorCode'=>0,'msg'=>'红包金额有误！');
            return($data);
        }
        //查询该红包是否已经存在
        $map = array();
        $map['scheme_id'] = $scheme_id;
        $map['platform_id'] = $platform_id;
        $map['user_id'] = $user_id;
        $packet = D('RedPacket')->get_one($map);
        if($packet){
            //红包存在则修改其设置的红包金额
            $map['id'] = $packet['id'];
            $data['user_award'] = $user_award;
            $data['push_award'] = $push_award;
            $r = D('RedPacket')->edit($map,$data);
            if($r){
                //红包设置成功需要添加操作记录（对账）
                $content = "用户设置".$platform_id."平台 方案ID：".$scheme_id."，名称".$scheme['scheme']."红包,用户返利金额为".$user_award."元，投顾剩余".$push_award."元。";
                R('Record/packet_log',array('user_id'=>$user_id,'content' => $content));
                $data = array('errorCode'=>1,'msg'=>'红包设置成功！');
            }else{
                $data = array('errorCode'=>2,'msg'=>'红包设置失败00a1！');
            }
        }else{

            //红包不存在新增一个红包
            $data['platform_id'] = $platform_id;
            $data['scheme_id'] = $scheme_id;
            $data['user_id'] = $user_id;
            $data['user_award'] = $user_award;
            $data['push_award'] = $push_award;
            $r = D('RedPacket')->add($data);
           
            if($r){
                 //红包设置成功需要添加操作记录（对账）
                $content = "用户设置".$platform_id."平台 方案ID：".$scheme_id."，名称".$scheme['scheme']."红包,用户返利金额为".$user_award."元，投顾剩余".$push_award."元。";
                R('Record/packet_log',array('user_id'=>$user_id,'content' => $content));
                $data = array('errorCode'=>1,'msg'=>'红包设置成功！'); 
            }else{
                $data = array('errorCode'=>2,'msg'=>'红包设置失败00a2！');
            }
        }
        return($data);
    }
}